package com.hz.sdka.ada;

import android.app.Activity;
import android.text.TextUtils;
import android.util.Log;
import android.view.ViewGroup;

import com.anythink.splashad.api.ATSplashAd;
import com.hz.sdka.commona.SDKContext;
import com.hz.sdka.commona.api.splash.OnHZSplashListener;
import com.hz.sdka.commona.bean.HZAdError;
import com.hz.sdka.commona.bean.HZAdInfo;
import com.hz.sdka.sdka.AdSplasha;

/**
 * topon  激励视屏
 */
public class Splasha extends AdBasea {
    private static final Splasha splash = new Splasha();
    private String TAG = "TopOnAd";
    private AdSplasha adSplash;
    private AdListenera adListener;

    public static Splasha getInstance() {
        return splash;
    }

    public void showAd(Activity activity, ViewGroup viewGroup, String placeId, String scenarioId) {
        if (adListener != null) adListener.adRequest();
        if (!TextUtils.isEmpty(scenarioId)) {
            ATSplashAd.entryAdScenario(placeId, scenarioId);
        }
        if (adSplash != null && adSplash.isReady()) {
            if (adListener != null) adListener.adReady();
            adSplash.show(activity, viewGroup, scenarioId);
        }
    }

    public double getEcpm() {
        if (adSplash != null) {
            adSplash.getEcpm();
        }
        return 0.00;
    }


    /*判断广告是否准备好*/
    public boolean isReady() {
        if (adSplash != null)
            return adSplash.isReady();
        else return false;
    }

    public void load(String placeId) {
        if (adSplash == null)
            adSplash = new AdSplasha(SDKContext.getInstance().getContext(), placeId);
        adSplash.setListener(new OnHZSplashListener() {
            @Override
            public void onSplashLoadFailed(HZAdError error) {
                Log.i(TAG, "onSplashLoadFailed:\n" + error.printStackTrace());
                if (adListener != null) adListener.adError(error);
            }

            @Override
            public void onSplashLoaded(boolean isTimeout) {
                Log.i(TAG, "onSplashLoaded:\n");
                if (adListener != null) adListener.adLoaded();
            }

            @Override
            public void onSplashShown(HZAdInfo hzAdInfo) {
                Log.i(TAG, "onSplashShown:\n");
                if (adListener != null) adListener.adShow();
            }

            @Override
            public void onSplashSkip() {
                Log.i(TAG, "onSplashSkip:\n");
            }

            @Override
            public void onSplashClosed() {
                Log.i(TAG, "onSplashClosed:\n");
                if (adListener != null) adListener.adClose();
            }

            @Override
            public void onSplashClicked() {
                Log.i(TAG, "onSplashClicked:\n");
            }

            @Override
            public void onSplashShowFailed(HZAdError error) {
                Log.i(TAG, "onSplashShowFailed:\n" + error.printStackTrace());
                if (adListener != null) adListener.adError(error);
            }
        });
        load();
    }


    public void setAdListener(AdListenera adListener) {
        this.adListener = adListener;
    }

    /**
     * 加载广告
     */
    private void load() {
        if (adSplash != null) {
            if (adListener != null) adListener.adLoading();
            adSplash.load();
        }
    }

}
